﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VideoNet.Common
{
    /// <summary>
    /// 海康摄像头
    /// </summary>
    public class CHCManage
    {
        private Int32 m_lUserID = -1;
        private Int32 m_lRealHandle = -1;
        private string _Ip = "192.168.0.64";
        private int _Port = 8000;
        private string _UserName = "admin";
        private string _PassWord = "sws888888";
        public CHCManage(string IP, int Port, string UserName, string PassWord)
        {
            _Ip = IP;
            _Port = Port;
            _UserName = UserName;
            _PassWord = PassWord;
        }
        public bool Login()
        {
            CHCNetSDK.NET_DVR_DEVICEINFO_V30 DeviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO_V30();
            //登录设备 Login the device
            m_lUserID = CHCNetSDK.NET_DVR_Login_V30(_Ip, _Port, _UserName, _PassWord, ref DeviceInfo);
            return m_lUserID >= 0;
        }
        public void Start(IntPtr realHandle)
        {
            CHCNetSDK.NET_DVR_PREVIEWINFO lpPreviewInfo = new CHCNetSDK.NET_DVR_PREVIEWINFO();
            lpPreviewInfo.hPlayWnd = realHandle;//预览窗口
            lpPreviewInfo.lChannel = 1;//预te览的设备通道
            lpPreviewInfo.dwStreamType = 0;//码流类型：0-主码流，1-子码流，2-码流3，3-码流4，以此类推
            lpPreviewInfo.dwLinkMode = 0;//连接方式：0- TCP方式，1- UDP方式，2- 多播方式，3- RTP方式，4-RTP/RTSP，5-RSTP/HTTP 
            lpPreviewInfo.bBlocked = true; //0- 非阻塞取流，1- 阻塞取流
            lpPreviewInfo.dwDisplayBufNum = 50; //播放库播放缓冲区最大缓冲帧数

            CHCNetSDK.REALDATACALLBACK RealData = new CHCNetSDK.REALDATACALLBACK(RealDataCallBack);//预览实时流回调函数
            IntPtr pUser = new IntPtr();//用户数据

            //打开预览 Start live view 
            m_lRealHandle = CHCNetSDK.NET_DVR_RealPlay_V40(m_lUserID, ref lpPreviewInfo, null/*RealData*/, pUser);
        }
        public void RealDataCallBack(Int32 lRealHandle, UInt32 dwDataType, IntPtr pBuffer, UInt32 dwBufSize, IntPtr pUser)
        {
            //下面数据处理建议使用委托的方式

        }
        public bool SaveBMP(string bmpPath)
        {
            return CHCNetSDK.NET_DVR_CapturePicture(m_lRealHandle, bmpPath);
        }
        public bool SaveJpg(string jpgPath)
        {
            CHCNetSDK.NET_DVR_JPEGPARA lpJpegPara = new CHCNetSDK.NET_DVR_JPEGPARA();
            lpJpegPara.wPicQuality = 0; //图像质量 Image quality
            lpJpegPara.wPicSize = 0xff; //抓图分辨率 Picture size: 2- 4CIF，0xff- Auto(使用当前码流分辨率)，抓图分辨率需要设备支持，更多取值请参考SDK文档
            return CHCNetSDK.NET_DVR_CaptureJPEGPicture(m_lUserID, 1, ref lpJpegPara, jpgPath);
        }
        public Image SaveImage()
        {
            uint iBuffSize = 10000000; //缓冲区大小需要不小于一张图片数据的大小 The buffer size should not be less than the picture size
            byte[] byJpegPicBuffer = new byte[iBuffSize];
            uint dwSizeReturned = 0;
            bool result = CHCNetSDK.NET_DVR_CapturePictureBlock_New(m_lRealHandle, byJpegPicBuffer, iBuffSize, ref dwSizeReturned);
            return byteArrayToImage(byJpegPicBuffer);
        }
        public Image SaveImageNew()
        {
            CHCNetSDK.NET_DVR_JPEGPARA lpJpegPara = new CHCNetSDK.NET_DVR_JPEGPARA();
            lpJpegPara.wPicQuality = 0; //图像质量 Image quality
            lpJpegPara.wPicSize = 0xff; //抓图分辨率 Picture size: 2- 4CIF，0xff- Auto(使用当前码流分辨率)，抓图分辨率需要设备支持，更多取值请参考SDK文档
            uint iBuffSize = 10000000; //缓冲区大小需要不小于一张图片数据的大小 The buffer size should not be less than the picture size
            byte[] byJpegPicBuffer = new byte[iBuffSize];
            uint dwSizeReturned = 0;
            bool result = CHCNetSDK.NET_DVR_CaptureJPEGPicture_NEW(m_lUserID, 1, ref lpJpegPara, byJpegPicBuffer, iBuffSize, ref dwSizeReturned);
            return byteArrayToImage(byJpegPicBuffer);
        }
        private Image byteArrayToImage(byte[] Bytes)
        {
            MemoryStream ms = new MemoryStream(Bytes);
            Image outputImg = Image.FromStream(ms);
            return outputImg;
        }
    }
}
